#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File: ali_crawl.py
@Time: 2023/02/20 23:43:34
@Description:  爬取阿里漏洞库漏洞名称
'''
import requests
import re
from lxml import etree
import warnings
import pprint
import time

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.91 Safari/537.36"} 
warnings.filterwarnings('ignore')

def get_cn_name(cve):
    """输入cve编号,返回漏洞详情"""
    info ={}
    url = "https://avd.aliyun.com/detail?id="+cve
    html = requests.get(url=url,headers=headers,verify=False)
    html.encoding="utf-8"
    html=etree.HTML(html.content, parser=etree.HTMLParser(encoding='utf-8'))  
    time.sleep(1)     
    try:
        cn_name = html.xpath("/html/body/div[1]/div[2]/div[1]/div[1]/h5/span/text()")[0]  # 漏洞中文标题
        describe = html.xpath("/html/body/div[3]/div/div[1]/div[2]/div[1]/div/text()")[0]  #漏洞描述，可能为英文
        advice = html.xpath("/html/body/div[3]/div/div[1]/div[2]/div[2]/text()")[0]  #解决建议
        link = html.xpath("""/html/body/div[3]/div/div[1]/div[2]/div[3]/table/tbody/tr[1]/td[1]/a/@href""")[0] #参考链接
   
    
        info['cn_name'] = cn_name
        info['describe'] = describe.strip()#清除多余换行字符
        info['adivce'] = advice.strip() #清除多余换行字符
        info['link'] = link
        return info
    except Exception as e:
        pass
# if __name__ == '__main__' :
#     information = get_cn_name("CVE-2022-0140")
#     pprint.pprint(information)
